package com.zjdiepu.www.biz.third;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.third.BaseShortMessageMapper;
import com.zjdiepu.www.ibiz.third.IBaseShortMessageService;
import com.zjdiepu.www.model.third.dto.BaseShortMessage;
import com.zjdiepu.www.model.third.ext.BaseShortMessageExt;

/**
 * Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年04月13日 17时08分<br/>
 * @since 1.0<br/>
 */
@Service
public class BaseShortMessageServiceImpl extends BaseService implements IBaseShortMessageService {
	public static final Logger logger = LoggerFactory.getLogger(BaseShortMessageServiceImpl.class);

	@Autowired
	private BaseShortMessageMapper baseShortMessageMapper;
	
	@Override
	public void saveUpdateAndDelete(BaseShortMessage baseShortMessage, OperateType operType) {
		switch (operType) {
		case add:
			baseShortMessage.initInsertData(baseShortMessage.getModifier(), baseShortMessage.getModifierName());
			baseShortMessageMapper.insert(baseShortMessage);
			break;
		case edit:
			baseShortMessage.initUpdateData(baseShortMessage.getModifier(), baseShortMessage.getModifierName());
			// 影响的记录数
			int updateCount = baseShortMessageMapper.updateByPrimaryKeySelective(baseShortMessage);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			baseShortMessage.initDeleteData(baseShortMessage.getModifier(), baseShortMessage.getModifierName());
			// 影响的记录数
			int deleteCount = baseShortMessageMapper.updateByPrimaryKeySelective(baseShortMessage);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<BaseShortMessage> selectByExt(BaseShortMessageExt baseShortMessageExt) {
		return baseShortMessageMapper.selectByExt(baseShortMessageExt);
	}
	
	@Override
	public int countByExt(BaseShortMessageExt baseShortMessageExt){
		return baseShortMessageMapper.countByExt(baseShortMessageExt);
	}
	
	@Override
	public BaseShortMessage selectById(String id) {
		return baseShortMessageMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			baseShortMessageMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(BaseShortMessage baseShortMessage) {
		return baseShortMessageMapper.countRefined(baseShortMessage);
	}

	@Override
	public BaseShortMessage selectRefined(BaseShortMessage baseShortMessage) {
		return baseShortMessageMapper.selectRefined(baseShortMessage);
	}

	@Override
	public boolean repeat(BaseShortMessage baseShortMessage) {
		int count = countRefined(baseShortMessage);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(baseShortMessage.getId())) {
			return true;
		}
		BaseShortMessage dbItem = selectRefined(baseShortMessage);
		if(null == dbItem) {
			return false;
		}
		return !baseShortMessage.getId().equals(dbItem.getId());
	}

	/* 
	* 所属公司：稻普控股集团
	* @author zhangpp
	* <p>Title: getMobileMessageUnixtimeService</p> 
	* <p>Description: </p> 
	* @param baseShortMessage
	* @return 
	* @see com.api.party.service.IBaseShortMessageService#getMobileMessageUnixtimeService(com.api.party.entity.model.BaseShortMessage) 
	* @version V1.0
	*/
	@Override
	public int getMobileMessageUnixtimeService(BaseShortMessage baseShortMessage) {
		
		return baseShortMessageMapper.getMobileMessageUnixtime(baseShortMessage);
	}

	/* 
	* 所属公司：稻普控股集团
	* @author zhangpp
	* <p>Title: getCountRowsIn30MinsService</p> 
	* <p>Description: </p> 
	* @param baseShortMessage
	* @return 
	* @see com.api.party.service.IBaseShortMessageService#getCountRowsIn30MinsService(com.api.party.entity.model.BaseShortMessage) 
	* @version V1.0
	*/
	@Override
	public int getCountRowsIn30MinsService(BaseShortMessage baseShortMessage) {
			
		return baseShortMessageMapper.getCountRowsIn30Mins(baseShortMessage);
	}
}
